package rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;

import rmi.hello.Hello;
import rmi.sync.FieldTuple;
import rmi.sync.ISyncMgr;

import common.Constant;
import ent.Ent1;
import ent.Ent2;

public class Client {

	private ISyncMgr syncMgr = null;
	
	public Client(){
		try {
			syncMgr = (ISyncMgr)Naming.lookup("rmi://127.0.0.1:" + Constant.PORT + "/syncMgr");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
 	public void compare(Ent1 ent1,Ent2 ent2){
		try {
			Hello hello = (Hello) Naming.lookup("rmi://127.0.0.1:" + Constant.PORT + "/hello");
			hello.compare(ent1, ent2);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setEnt(Ent1 ent1,Ent2 ent2){
		try {
			Hello hello = (Hello) Naming.lookup("rmi://127.0.0.1:" + Constant.PORT + "/hello");
			hello.setEnt1(ent1);
			hello.setEnt2(ent2);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Object readRemote(int id){
		Object obj = null;
		try {
			obj = syncMgr.syncToClient(id);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	
	public void writeRemote(int id, Object obj){
		try {
			syncMgr.syncToServer(id, obj);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<FieldTuple> readContent(int id){
		List<FieldTuple> ret = null;
		try {
			ret = syncMgr.syncContentToC(id);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
